/* In 2's complement form the largest positive number with wordsize w is (2^w -1).However in the itoa() the largest negative number i.e -2^w is attempted to be converted to 2^w and stored in an integer which exceeds the limit */
#include<stdio.h>
#include<string.h>
#include<limits.h>
void itoa(int ,char []);
void reverse(char []);
int main()
{
  int n;
  char s[20];
  
  printf("enter a number");
  scanf("%d",&n);
  itoa(n,s);
  printf("the number stored as string is" );
  puts(s);
  return 0;
}
void itoa(int n,char s[])
{
  int i,sign,flag=0;
 
  if(n==INT_MIN)
  {
   n=(-n)-1; 
   flag=1;
  }
  else
  if((sign=n)<0)
    n=-n;
    
    i=0;
    if(flag==1 && i==0)
      {
        s[i++]=(n%10 +1) +'0';
        n=n/10;
      } 
    do{
       s[i++]=n%10 + '0';
      }
    while((n=n/10)>0);
    if(sign<0 || flag==1)
       s[i++]='-';
       s[i]='\0';
    reverse(s);
}
void reverse(char s[])
{
  char temp;
  int i,j;

  for(i=0,j=strlen(s)-1;i<j;i++,j--)
    {
      temp=s[i];
      s[i]=s[j];
      s[j]=temp;
    }   
}
 


  
  
